﻿using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ChuanYe.CountyFireManagement.BLL
{
    public class RecordBLL
    {
        #region 构造单例
        private RecordBLL() { }
        private static RecordBLL _instance;
        public static RecordBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new RecordBLL());
            }
        }
        #endregion


        public List<LastRecordModel> GetLast(List<int> fireIds, List<int> ignoreRecordTypes)
        {
            var sql = AppSetting.GetConfig("Sql:RecordBLL.GetLast");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql =string.Empty;
                if (fireIds != null && fireIds.Count > 0)
                {
                    string fireIDsParms =string.Join(",", fireIds);
                    paramsSql += " and  FireID in({0})  ";
                    paramsSql = string.Format(paramsSql, fireIDsParms);
                }
                if (ignoreRecordTypes != null && ignoreRecordTypes.Count > 0)
                {
                    string ignoreRecordTypesParms =string.Join(",", ignoreRecordTypes);
                    paramsSql += "  and Type not in ({0})  ";
                    paramsSql = string.Format(paramsSql, ignoreRecordTypesParms);
                }
                sql = string.Format(sql, paramsSql);
                var list = ef.Database.SqlQuery<LastRecordModel>(sql);
                return list;
            }
        }

        public ResultDto<dynamic,dynamic> Get(int fireId, List<int> ignoreRecordTypes)
        {
            List<Record> list = null;
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Record select a;
                linq = linq.Where(p => p.FireId == fireId);
                if (ignoreRecordTypes != null && ignoreRecordTypes.Count > 0)
                {
                    linq = linq.Where(p => !ignoreRecordTypes.Contains(p.Type.Value));
                }
                list = linq.OrderByDescending(o => o.RecordId).ToList();

                //进度条
                var bar = list.OrderBy(o => o.RecordId).Select(s => new { s.Type,s.CustomData }).ToList();
                result.FirstParam = list;
                result.SecondParam = bar;
                return result;
            }
        }

        public List<LastRecordModel> GetLast(int fireId, List<int> ignoreRecordTypes) 
        {
            List<int> fireIds = new List<int>() { fireId };
            return this.GetLast(fireIds, ignoreRecordTypes);
        }

        public int Add(Record info,List<int> ignoreRecordTypes)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Record select a;
                linq = linq.Where(p => p.FireId == info.FireId && p.Type == info.Type);
                if (ignoreRecordTypes != null && ignoreRecordTypes.Count > 0)
                {
                    linq = linq.Where(p => !ignoreRecordTypes.Contains(p.Type.Value));
                }
                var exist = linq.FirstOrDefault();
                if (exist != null)
                {
                    return 0;
                }

                ef.Record.Add(info);
                ef.Entry<Record>(info).State = EntityState.Added;
                ef.SaveChanges();
                return info.RecordId;
            }
        }
    }
}
